package com.lzxuni.modules.pestdetails.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.pestdetails.entity.PestDisastermanagement;
import com.lzxuni.modules.pestdetails.mapper.PestDisastermanagementMapper;
import com.lzxuni.modules.pestdetails.service.PestDisastermanagementService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 虫害信息管理 服务实现类
 * </p>
 *
 * @author rdf
 * @since 2020-12-07
 */
@Service
public class PestDisastermanagementServiceImpl extends ServiceImpl<PestDisastermanagementMapper, PestDisastermanagement> implements PestDisastermanagementService {

    @Override
    public PageInfo<PestDisastermanagement> queryPage(PageParameter pageParameter, PestDisastermanagement pestDisastermanagement) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    pageParameter.getSidx() + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<PestDisastermanagement> pestDisastermanagementList = baseMapper.queryList(pestDisastermanagement);
        PageInfo<PestDisastermanagement> pageInfo = new PageInfo<>(pestDisastermanagementList);
        return pageInfo;
    }
//导出
    @Override
    public Workbook exportExcel(PestDisastermanagement pestDisastermanagement) throws IOException {
        PageHelper.orderBy("happen_time" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        List<PestDisastermanagement> pesList =baseMapper.queryList(pestDisastermanagement);
        List<String> list0 = new ArrayList<>();
        list0.add("地块名称");
        list0.add("发生时间");
        list0.add("虫害名称");


        listData1.add(list0);
        if (pesList != null && pesList.size() > 0) {
            for (int i = 0; i < pesList.size(); i++) {
                PestDisastermanagement pessm = pesList.get(i);
                List<String> list = new ArrayList<>();

                list.add(pessm.getLandNum());
                list.add(DateFormatUtils.format(pessm.getHappenTime(),"yyyy-MM-dd"));
                list.add(pessm.getPestsName());

                listData1.add(list);
            }
        }
        dataMap.put("虫情灾害信息", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "虫情灾害信息");
        return wb;
    }
}
